
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif




#define full_width 128
#define full_height 64
static unsigned char full_bits[] = {

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x38, 0x04, 0x0F, 0xFE, 0x03, 0x00, 0x70, 0x1C,
  0x10, 0x0E, 0x00, 0x80, 0x7B, 0x20, 0xF8, 0x3F, 0x44, 0xC4, 0x30, 0x06,
  0x03, 0x00, 0x88, 0x22, 0x18, 0x01, 0x00, 0x00, 0x89, 0x68, 0x08, 0x20,
  0x28, 0x25, 0x40, 0x8A, 0x02, 0x00, 0xC8, 0x22, 0x11, 0x01, 0x00, 0x00,
  0x89, 0xB0, 0xA8, 0x6A, 0x10, 0x05, 0x0F, 0x52, 0x02, 0x00, 0xA8, 0x1C,
  0x10, 0x0F, 0x00, 0x00, 0x79, 0x60, 0xA8, 0x6A, 0x50, 0x85, 0x10, 0x22,
  0x02, 0x00, 0x98, 0x22, 0x11, 0x11, 0x00, 0x00, 0x29, 0xB0, 0xA8, 0x6A,
  0x50, 0x05, 0x00, 0x02, 0x02, 0x00, 0x88, 0x22, 0x10, 0x11, 0x00, 0x00,
  0x49, 0x68, 0x08, 0x20, 0x50, 0x05, 0x06, 0xFE, 0x03, 0x00, 0x70, 0x1C,
  0x38, 0x0E, 0x00, 0x80, 0x8B, 0x20, 0xF8, 0x3F, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xF8, 0x0F, 0xC7, 0x21, 0x0E, 0xE0, 0x10, 0x40, 0x1C, 0xF8, 0x1E, 0x87,
  0x00, 0x88, 0x08, 0x00, 0x28, 0x8A, 0x28, 0x32, 0x11, 0x10, 0x19, 0x60,
  0x22, 0x08, 0x22, 0x82, 0x00, 0x90, 0x04, 0x00, 0x08, 0x08, 0x28, 0x23,
  0x11, 0x90, 0x11, 0x40, 0x22, 0x08, 0x22, 0x82, 0x00, 0xC0, 0x01, 0x00,
  0xA8, 0x0A, 0xA7, 0x22, 0x9E, 0x53, 0x11, 0x47, 0x1C, 0x78, 0x1E, 0x82,
  0x00, 0x20, 0x02, 0x00, 0x08, 0x88, 0x60, 0x22, 0x10, 0x30, 0x11, 0x40,
  0x22, 0x08, 0x0A, 0x82, 0x00, 0x38, 0x0E, 0x00, 0xA8, 0x8A, 0x20, 0x22,
  0x10, 0x10, 0x11, 0x40, 0x22, 0x08, 0x12, 0x82, 0x00, 0x20, 0x02, 0x00,
  0x08, 0x88, 0xCF, 0x71, 0x0E, 0xE0, 0x38, 0xE0, 0x1C, 0x08, 0x22, 0x87,
  0x00, 0xC0, 0x01, 0x00, 0xF8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x90, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x88, 0x08, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x01, 0x9C, 0x39,
  0xB8, 0x07, 0xE2, 0x38, 0x40, 0x38, 0x0E, 0x90, 0x8F, 0x00, 0x71, 0x9C,
  0x80, 0x01, 0x02, 0x44, 0x90, 0x08, 0x13, 0x45, 0x60, 0x04, 0x11, 0x18,
  0xC8, 0x80, 0x89, 0x82, 0x40, 0x11, 0x02, 0x04, 0x90, 0x08, 0x12, 0x41,
  0x40, 0x04, 0x11, 0x10, 0x84, 0x00, 0x81, 0x82, 0x2E, 0xA9, 0x1E, 0x04,
  0x90, 0x07, 0xE2, 0x39, 0x40, 0x3C, 0x0E, 0x10, 0x86, 0x00, 0x71, 0x9E,
  0xE0, 0x43, 0x22, 0x04, 0x90, 0x00, 0x02, 0x05, 0x40, 0x44, 0x11, 0x10,
  0x88, 0x00, 0x09, 0xA2, 0x00, 0x01, 0x22, 0x44, 0x90, 0x00, 0x02, 0x05,
  0x46, 0x44, 0x91, 0x91, 0x88, 0x18, 0x09, 0xA2, 0x00, 0x01, 0x1C, 0x38,
  0xB8, 0x00, 0xE7, 0x7C, 0xE6, 0x38, 0x8E, 0x39, 0xCF, 0x99, 0xFB, 0x9C,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0x00, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0xE0, 0xC1, 0x80, 0x01, 0x50, 0x01, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x10, 0xC2, 0x81, 0x02,
  0x00, 0x80, 0xCF, 0x79, 0x3E, 0x07, 0x1C, 0x38, 0x88, 0x13, 0x45, 0x80,
  0x48, 0xC4, 0x83, 0x04, 0xF4, 0x85, 0x20, 0x8A, 0xA0, 0x08, 0x22, 0x44,
  0x4C, 0xB4, 0x45, 0x80, 0x48, 0xC4, 0x87, 0x08, 0x10, 0x81, 0x20, 0x88,
  0x10, 0x08, 0x3A, 0x40, 0x4A, 0x56, 0x45, 0x9F, 0xC8, 0xC5, 0x8F, 0x10,
  0x14, 0x85, 0xC7, 0x79, 0x18, 0x07, 0x2A, 0x38, 0x49, 0x55, 0x7D, 0x88,
  0x08, 0xC4, 0x83, 0x08, 0x10, 0x81, 0x00, 0x0A, 0xA0, 0x00, 0x1A, 0x04,
  0xDF, 0x14, 0x45, 0x84, 0x10, 0xC2, 0x82, 0x06, 0xF4, 0x85, 0x20, 0x0A,
  0xA2, 0x00, 0x02, 0x04, 0x48, 0x14, 0x45, 0x82, 0xE0, 0x01, 0x82, 0x05,
  0x00, 0x80, 0xCF, 0x09, 0xBC, 0x0F, 0x3C, 0x7C, 0x88, 0x13, 0x45, 0x9F,
  0x00, 0x00, 0x04, 0x08, 0x50, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00,
  0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x03, 0x04,
  0xE0, 0x03, 0x04, 0xF0, 0x1F, 0x40, 0x00, 0x08, 0x40, 0x00, 0x49, 0x00,
  0x00, 0x01, 0x04, 0x04, 0xF0, 0x07, 0x04, 0x10, 0x10, 0x40, 0x80, 0x28,
  0x40, 0x80, 0x4B, 0x00, 0x00, 0x09, 0x04, 0x04, 0x10, 0x04, 0x04, 0x50,
  0x10, 0x40, 0x40, 0x44, 0x40, 0x00, 0x49, 0x00, 0x00, 0x1D, 0x04, 0x04,
  0x10, 0x04, 0x04, 0x90, 0x10, 0x40, 0x20, 0x84, 0x40, 0x00, 0xE9, 0x00,
  0x00, 0x49, 0x05, 0x04, 0x38, 0x0E, 0x04, 0x50, 0x16, 0x40, 0x40, 0x44,
  0x40, 0x00, 0x49, 0x00, 0x00, 0x01, 0x04, 0x04, 0x38, 0x0E, 0x04, 0x10,
  0x10, 0x40, 0x80, 0x22, 0x40, 0x00, 0x5D, 0x00, 0x00, 0xFE, 0x03, 0x04,
  0x38, 0x0E, 0x04, 0xF0, 0x1F, 0x40, 0x00, 0x02, 0x40, 0x00, 0x49, 0x00,
  0x00, 0x00, 0x00, 0x04, 0x30, 0x06, 0x04, 0x00, 0x00, 0x40, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00,
  0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x3C, 0x22, 0xFA, 0x44,
  0xF4, 0x7C, 0xE4, 0x21, 0x9C, 0x48, 0x38, 0xCF, 0x47, 0xCF, 0xF3, 0x7D,
  0x22, 0x65, 0x0B, 0xC4, 0x16, 0x41, 0x24, 0x52, 0xA2, 0x48, 0x10, 0x51,
  0x40, 0x51, 0x14, 0x04, 0x82, 0xA8, 0x0A, 0x44, 0x15, 0x21, 0x24, 0x8A,
  0x82, 0x48, 0x10, 0x51, 0x40, 0x51, 0x14, 0x04, 0x82, 0xA8, 0x7A, 0x44,
  0xF5, 0x30, 0xE4, 0x89, 0x9C, 0x4F, 0x10, 0xD1, 0x43, 0xCF, 0xF3, 0x3C,
  0xB2, 0x2F, 0x0A, 0x44, 0x14, 0x40, 0x24, 0xFA, 0xA0, 0x48, 0x10, 0x51,
  0x40, 0x41, 0x11, 0x04, 0xA2, 0x28, 0x0A, 0x44, 0x14, 0x44, 0x24, 0x8A,
  0xA2, 0x48, 0x10, 0x51, 0x40, 0x41, 0x12, 0x04, 0xBC, 0x28, 0xFA, 0x44,
  0x14, 0x78, 0xE4, 0x89, 0x9C, 0x48, 0x38, 0xCF, 0x47, 0x41, 0xF4, 0x05,
  0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00,
};

/*
  U8glib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.

*/
U8G2_SH1107_64X128_F_4W_HW_SPI u8g2(U8G2_R3, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 33);

// End of constructor list


void u8g2_prepare(void) {
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}


#define cross_width 24
#define cross_height 24
static const unsigned char cross_bits[] U8X8_PROGMEM  = {
  0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00,
  0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00,
  0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80,
  0x01, 0x42, 0x80, 0x06, 0x42, 0x60, 0x38, 0x3C, 0x1C, 0xC0, 0x00, 0x03,
  0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00,
  0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00,
};

#define cross_fill_width 24
#define cross_fill_height 24
static const unsigned char cross_fill_bits[] U8X8_PROGMEM  = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26,
  0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08,
  0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01,
  0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04,
  0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21,
  0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

#define cross_block_width 14
#define cross_block_height 14
static const unsigned char cross_block_bits[] U8X8_PROGMEM  = {
  0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
  0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
  0x01, 0x20, 0xFF, 0x3F,
};

void u8g2_bitmap_overlay(uint8_t a) {
  uint8_t frame_size = 28;

  u8g2.drawStr(0, 0, "Bitmap overlay");

  u8g2.drawStr(0, frame_size + 12, "Solid / transparent");
  u8g2.setBitmapMode(false /* solid */);
  u8g2.drawFrame(0, 10, frame_size, frame_size);
  u8g2.drawXBMP(2, 12, cross_width, cross_height, cross_bits);
  if (a & 4)
    u8g2.drawXBMP(7, 17, cross_block_width, cross_block_height, cross_block_bits);

  u8g2.setBitmapMode(true /* transparent*/);
  u8g2.drawFrame(frame_size + 5, 10, frame_size, frame_size);
  u8g2.drawXBMP(frame_size + 7, 12, cross_width, cross_height, cross_bits);
  if (a & 4)
    u8g2.drawXBMP(frame_size + 12, 17, cross_block_width, cross_block_height, cross_block_bits);
}

void u8g2_bitmap_modes(uint8_t transparent) {
  const uint8_t frame_size = 24;

  if (!transparent) {
    u8g2.setBitmapMode(false /* solid */);
    u8g2.drawStr(0, 0, "Solid bitmap");
  } else {
    u8g2.setBitmapMode(true /* transparent*/);
    u8g2.drawStr(0, 0, "Transparent bitmap");
  }
  u8g2.setDrawColor(0);// Black
  u8g2.drawXBMP(frame_size * 0.5, 24, cross_width, cross_height, cross_bits);
  u8g2.setDrawColor(1); // White
  u8g2.drawXBMP(frame_size * 2, 24, cross_width, cross_height, cross_bits);
  u8g2.setDrawColor(2); // XOR
  u8g2.drawXBMP(frame_size * 3.5, 24, cross_width, cross_height, cross_bits);
}

void u8g2_bitmap_full() {
  uint8_t transparent = 0;
  if (!transparent) {
    u8g2.setBitmapMode(false /* solid */);
    u8g2.drawStr(0, 0, "Solid bitmap");
  } else {
    u8g2.setBitmapMode(true /* transparent*/);
    u8g2.drawStr(0, 0, "Transparent bitmap");
  }
  u8g2.setDrawColor(1); // White
  // u8g2.setDrawColor(0);// Black
  u8g2.drawXBMP(0, 0, full_width, full_height, full_bits);
  //  u8g2.drawXBMP(frame_size * 2, 24, cross_width, cross_height, cross_bits);
  //  u8g2.setDrawColor(2); // XOR
  //  u8g2.drawXBMP(frame_size * 3.5, 24, cross_width, cross_height, cross_bits);
}


uint8_t draw_state = 0;

void draw(void) {
  u8g2_prepare();

  u8g2_bitmap_full() ;

}

void setup(void) {
  u8g2.begin();
}

void loop(void) {
  // picture loop
  u8g2.clearBuffer();
  draw();
  u8g2.sendBuffer();

  // increase the state
  draw_state++;

  // deley between each page
  // delay(1000);

  u8g2.setContrast(32);

}
